#define _CRT_SECURE_NO_WARNINGS
#include "Heap.h"

void TestHeap01()
{
	int array[] = { 27,15,19,18,28,34,65,49,25,37 };
	Heap sl;
	HeapInit(&sl);
	int len = sizeof(array) / sizeof(array[0]);
	for (int i = 0; i < len; i++)
	{
		HeapPush(&sl, array[i]);
	}
	HeapPrint(&sl);

	HeapPop(&sl);
	HeapPrint(&sl);

	/*while (!HeapEmpty(&sl))
	{
		printf("%d ", HeapTop(&sl));
		HeapPop(&sl);
	}*/
}

void TestHeap02()
{
	int array[] = { 27,15,19,18,28,34,65,49,25,37 };
	Heap sl;
	int size = sizeof(array) / sizeof(array[0]);
	HeapCreate(&sl, array, size);
	HeapPrint(&sl);
}

void HeapSort(int* arr, int n)
{
	int parentNode = (n - 1 - 1) / 2;

	for (int i = parentNode; i >= 0; i--)
	{
		AdjustDown(arr, n, i);
	}

	int end = n - 1;
	while (end > 0)
	{
		HeapSwap(&arr[0], &arr[end]);
		AdjustDown(arr, end, 0);
		--end;
	}
}

void TestHeap03()
{
	int array[] = { 27,15,19,18,28,34,65,49,25,37 };
	int size = sizeof(array) / sizeof(array[0]);
	HeapSort(array, size);
	
	for (int i = 0; i < size; i++)
	{
		printf("%d ", array[i]);
	}
	printf("\n");
}

void TestHeap04()
{
	int array[] = { 27,15,19,18,28,34,65,49,25,37 };
	int size = sizeof(array) / sizeof(array[0]);
	HeapSort(array, size);

	for (int i = size - 1; i >= 0; i--)
	{
		printf("%d ", array[i]);
	}
	printf("\n");
}

void TestHeap05()
{
	int minHeap[5];
	FILE* fout = fopen("Data.txt","r");
	if (fout == NULL)
	{
		perror("fopen fail");
		return;
	}

	int k = 5;
	for (int i = 0; i < k; i++)
	{
		fscanf(fout, "%d", &minHeap[i]);
	}
	
	int parentNode = (k - 1 - 1) / 2;
	for (int i = parentNode; i >= 0; i--)
	{
		AdjustDown(minHeap, k, i);
	}

	int val = 0;
	while (fscanf(fout, "%d", &val) != EOF)
	{
		if (val > minHeap[0])
		{
			minHeap[0] = val;
			AdjustDown(minHeap, k, 0);
		}
	}

	for (int i = 0; i < k; i++)
	{
		printf("%d ", minHeap[i]);
	}

}

void TestHeap06()
{
	int array[] = { 27,15,19,18,28,34,65,49,25,37 };
	int size = sizeof(array) / sizeof(array[0]);
	
	
}

int main()
{
	//TestHeap01();
	//TestHeap03();
	TestHeap04();

	//TestHeap02();
	//TestHeap03();
	//TestHeap04();

	return 0;
}